package com.huwei.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Protocol;

@Component
@PropertySource(value= {"classpath:config/properties/pro-redis.properties"} ,encoding="utf8")
public class RedisProperties {
	
	@Value("${redis.clientName}")
	private String clientName;
	
	@Value("${redis.hostName}")
	private String host;
	
	@Value("${redis.port}")
	private String port;
	
	final static private int DEFAULT_PORT = Protocol.DEFAULT_PORT;
	
	@Value("${redis.password}")
	private String password;
	
	@Value("${redis.timeout}")
	private String timeout;
	
	final static private int DEFAULT_TIMEOUT = Protocol.DEFAULT_TIMEOUT; 
	
    @Value("${redis.maxIdle}")
    private Integer maxIdle;

    @Value("${redis.maxTotal}")
    private Integer maxTotal;
    
    @Value("${redis.minIdle}")
    private Integer minIdle;

    @Value("${redis.maxWaitMillis}")
    private Integer maxWaitMillis;

    @Value("${redis.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;

    @Value("${redis.numTestsPerEvictionRun}")
    private Integer numTestsPerEvictionRun;

    @Value("${redis.timeBetweenEvictionRunsMillis}")
    private long timeBetweenEvictionRunsMillis;

    @Value("${redis.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${redis.testWhileIdle}")
    private boolean testWhileIdle;


    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes; 

    @Value("${spring.redis.cluster.max-redirects}")
    private Integer mmaxRedirectsac;

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * @return the port
	 */
	public String getPort() {
		return port;
	}

	/**
	 * @param port the port to set
	 */
	public void setPort(String port) {
		this.port = port;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the timeout
	 */
	public String getTimeout() {
		return timeout;
	}

	/**
	 * @param timeout the timeout to set
	 */
	public void setTimeout(String timeout) {
		this.timeout = timeout;
	}

	/**
	 * @return the maxIdle
	 */
	public Integer getMaxIdle() {
		return maxIdle;
	}

	/**
	 * @param maxIdle the maxIdle to set
	 */
	public void setMaxIdle(Integer maxIdle) {
		this.maxIdle = maxIdle;
	}

	/**
	 * @return the maxTotal
	 */
	public Integer getMaxTotal() {
		return maxTotal;
	}

	/**
	 * @param maxTotal the maxTotal to set
	 */
	public void setMaxTotal(Integer maxTotal) {
		this.maxTotal = maxTotal;
	}

	/**
	 * @return the maxWaitMillis
	 */
	public Integer getMaxWaitMillis() {
		return maxWaitMillis;
	}

	/**
	 * @param maxWaitMillis the maxWaitMillis to set
	 */
	public void setMaxWaitMillis(Integer maxWaitMillis) {
		this.maxWaitMillis = maxWaitMillis;
	}

	/**
	 * @return the minEvictableIdleTimeMillis
	 */
	public Integer getMinEvictableIdleTimeMillis() {
		return minEvictableIdleTimeMillis;
	}

	/**
	 * @param minEvictableIdleTimeMillis the minEvictableIdleTimeMillis to set
	 */
	public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {
		this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
	}

	/**
	 * @return the numTestsPerEvictionRun
	 */
	public Integer getNumTestsPerEvictionRun() {
		return numTestsPerEvictionRun;
	}

	/**
	 * @param numTestsPerEvictionRun the numTestsPerEvictionRun to set
	 */
	public void setNumTestsPerEvictionRun(Integer numTestsPerEvictionRun) {
		this.numTestsPerEvictionRun = numTestsPerEvictionRun;
	}

	/**
	 * @return the timeBetweenEvictionRunsMillis
	 */
	public long getTimeBetweenEvictionRunsMillis() {
		return timeBetweenEvictionRunsMillis;
	}

	/**
	 * @param timeBetweenEvictionRunsMillis the timeBetweenEvictionRunsMillis to set
	 */
	public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
		this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
	}

	/**
	 * @return the testOnBorrow
	 */
	public boolean isTestOnBorrow() {
		return testOnBorrow;
	}

	/**
	 * @param testOnBorrow the testOnBorrow to set
	 */
	public void setTestOnBorrow(boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}

	/**
	 * @return the testWhileIdle
	 */
	public boolean isTestWhileIdle() {
		return testWhileIdle;
	}

	/**
	 * @param testWhileIdle the testWhileIdle to set
	 */
	public void setTestWhileIdle(boolean testWhileIdle) {
		this.testWhileIdle = testWhileIdle;
	}

	/**
	 * @return the clusterNodes
	 */
	public String getClusterNodes() {
		return clusterNodes;
	}

	/**
	 * @param clusterNodes the clusterNodes to set
	 */
	public void setClusterNodes(String clusterNodes) {
		this.clusterNodes = clusterNodes;
	}

	/**
	 * @return the mmaxRedirectsac
	 */
	public Integer getMmaxRedirectsac() {
		return mmaxRedirectsac;
	}

	/**
	 * @param mmaxRedirectsac the mmaxRedirectsac to set
	 */
	public void setMmaxRedirectsac(Integer mmaxRedirectsac) {
		this.mmaxRedirectsac = mmaxRedirectsac;
	}

	/**
	 * @return the minIdle
	 */
	public Integer getMinIdle() {
		return minIdle;
	}

	/**
	 * @param minIdle the minIdle to set
	 */
	public void setMinIdle(Integer minIdle) {
		this.minIdle = minIdle;
	}

	public RedisProperties() {
		super();
	}

	/**
	 * @return the clientName
	 */
	public String getClientName() {
		return clientName;
	}

	/**
	 * @param clientName the clientName to set
	 */
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public RedisProperties(String clientName, String host, String port, String password, String timeout,
			Integer maxIdle, Integer maxTotal, Integer minIdle, Integer maxWaitMillis,
			Integer minEvictableIdleTimeMillis, Integer numTestsPerEvictionRun, long timeBetweenEvictionRunsMillis,
			boolean testOnBorrow, boolean testWhileIdle, String clusterNodes, Integer mmaxRedirectsac) {
		super();
		this.clientName = clientName;
		this.host = host;
		this.port = port;
		this.password = password;
		this.timeout = timeout;
		this.maxIdle = maxIdle;
		this.maxTotal = maxTotal;
		this.minIdle = minIdle;
		this.maxWaitMillis = maxWaitMillis;
		this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
		this.numTestsPerEvictionRun = numTestsPerEvictionRun;
		this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
		this.testOnBorrow = testOnBorrow;
		this.testWhileIdle = testWhileIdle;
		this.clusterNodes = clusterNodes;
		this.mmaxRedirectsac = mmaxRedirectsac;
	}

	public int getPortOfTypeForInteger() {
		int result = DEFAULT_PORT;
		try {
			result = Integer.parseInt(port);
		}catch (Exception e) {
			// debug
		}
		
		return result;
	}

	public int getTimeoutOfTypeForInteger() {
		int result = DEFAULT_TIMEOUT;
		try {
			result = Integer.parseInt(timeout);
		}catch (Exception e) {
			// debug
		}
		
		return result;
	}

	
}
